package utils;

import domain.igra2Station;
import domain.txtData;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import static java.lang.Double.parseDouble;

/**
 * 完成于2021-8-4-10:24
 * 之前使用Matlab将ra数据提取出存到了txt格式的文件里面。
 * 本工具类就是将txt文件读进来，将一个txt的数据存入到一个txtData对象里面。
 * 这里面的try，catch太多了，肯定是可以精简的，但是，我这会懒得搞，如果有大哥帮忙指出，万分感谢
 */

public class readRaStandTxtUtil {
    /**
     * @param fileName 将要读的txt的文件名
     * @return 返回了一个txtData对象，包含掩星的经度，纬度，发生时间，以及经过三次样条插值过后的海拔，以及温度数据
     */
    public static txtData getTxtData(String fileName, String stationListName) {
        File file = new File(fileName);
        List<igra2Station> igra2StationList = getIgra2StationList(stationListName);
        txtData txtData = new txtData();
        try {
            if (file.isFile() && file.exists()) {
                InputStreamReader read;
                read = new InputStreamReader(new FileInputStream(file));
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt;
                int i = 0;
                ArrayList<Double> altList = new ArrayList<Double>();
                ArrayList<Double> tempList = new ArrayList<Double>();
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    //业务代码,如果lineTxt的长度大于多少，就记录
                    i++;
                    if (i == 1) {
                        //这里还有待优化，因为把格式定死了
                        int intYear = Integer.parseInt(lineTxt.substring(12, 18).trim());
                        //做的好一点，判断这个值是否存在
                        int intMonth = Integer.parseInt(lineTxt.substring(18, 21).trim());
                        int intDay = Integer.parseInt(lineTxt.substring(21, 24).trim());
                        int intHour = Integer.parseInt(lineTxt.substring(24, 27).trim());
                        int intMinute = Integer.parseInt(lineTxt.substring(27, 29).trim());
                        int intSecond = Integer.parseInt(lineTxt.substring(29, 32).trim());
                        txtData.setYear(intYear);
                        txtData.setMonth(intMonth);
                        txtData.setDay(intDay);
                        txtData.setHour(intHour);
                        txtData.setMinute(intMinute);
                        txtData.setSecond(intSecond);
                        String station = lineTxt.substring(1, 12);
                        assert igra2StationList != null;
                        for (igra2Station igra2Station : igra2StationList) {
                            if (igra2Station.getName().equals(station)) {
                                txtData.setLat(igra2Station.getLat());
                                txtData.setLon(igra2Station.getLon());
                            }
                        }
                    }
                    if (i > 1) {
                        String alt = lineTxt.substring(0, lineTxt.indexOf(" ") + 1);
                        String temp = lineTxt.substring(lineTxt.indexOf(" ") + 1);
                        double doubleAlt = parseDouble(alt.trim());
                        double doubleTemp = parseDouble(temp.trim());
                        altList.add(doubleAlt);
                        tempList.add(doubleTemp);
                    }
                }
                txtData.setAlt(altList);
                txtData.setTemp(tempList);
                read.close();
                return txtData;
            }
        } catch (Exception ignored) {
        }
        System.out.println("没数据");
        return null;


    }


    /**
     * @param fileName 存放探空气球站点数据的txt文档，可以写死。
     * @return 返回一个存放站点数据类的集合
     */
    public static List<igra2Station> getIgra2StationList(String fileName) {
        File file = new File(fileName);
        try {
            if (file.isFile() && file.exists()) {
                InputStreamReader read;
                read = new InputStreamReader(new FileInputStream(file));
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt;
                ArrayList<igra2Station> stationList = new ArrayList<igra2Station>();
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    igra2Station igra2Station = new igra2Station();
                    igra2Station.setName(lineTxt.substring(0, 11));
                    igra2Station.setLat(parseDouble(lineTxt.substring(12, 18)));
                    igra2Station.setLon(parseDouble(lineTxt.substring(21, 28)));
                    stationList.add(igra2Station);
                }
                return stationList;
            }
        } catch (Exception ignored) {
        }
        return null;
    }

}
